package com.bobohost.crmrbac.web.controller;

import com.bobohost.crmrbac.pojo.po.Department;
import com.bobohost.crmrbac.pojo.po.Employee;
import com.bobohost.crmrbac.pojo.po.Role;
import com.bobohost.crmrbac.pojo.query.EmployeeQuery;
import com.bobohost.crmrbac.service.DepartmentService;
import com.bobohost.crmrbac.service.EmployeeService;
import com.bobohost.crmrbac.service.RoleService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 波波老师
 * @since 2024-12-20
 */
@Controller
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    //注入员工service
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private RoleService roleService;

     /**
     *
     * @param model 给页面提升数据的数据模型
     * @param employeeQuery 查询参数的封装  ?currentPage =1 & pageSize =2 前面默认省略了@RequestParam
     * @return
     */
    @RequestMapping("/list")  //默认是get请求
    public String list(Model model, @ModelAttribute("qo") EmployeeQuery employeeQuery){

        log.debug("查询对象："+employeeQuery);

        //----------PageHelper的分页插件---必须放在列表查询的前面
//        //参数1：当前页码  参数2：每页最大记录数  ---前端传过来
//        PageHelper.startPage(employeeQuery.getCurrentPage(),employeeQuery.getPageSize());
//
//        //正常查询所有部门列表的
//        List<Employee> list = employeeService.list();
//
//        //封装普通列表为分页列表
//        PageInfo<Employee> pageInfo = new PageInfo<>(list);

        //------------------------部门的下拉列表填充
        //查询所有部门列表
        List<Department> departmentList = departmentService.list();
        model.addAttribute("departments", departmentList);

        //------------------------员工的分页列表查询+组合条件
        //调用业务层
        PageInfo<Employee> pageInfo = employeeService.findEmployeeListPage(employeeQuery);

        //在数据模型中将数据放进去
        model.addAttribute("pageInfo",pageInfo);

        return "employee/list";
    }

    @RequestMapping("/input")
    public String input(Model model, @RequestParam("id") Long id){
        if(id != null){
            Employee employee = employeeService.getById(id);
            List<Role> empRoleList=  roleService.findListByEmploeeId(id);
            employee.setRoles(empRoleList);
            model.addAttribute("employee",employee);

        }
        model.addAttribute("departments",departmentService.list());

        List<Role> roleList=  roleService.list();
        model.addAttribute("roles",roleList);
        return "employee/input";




    }



}
